package net.ix.opal.svrtest.aidl;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;

import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import net.ix.opal.R;

public class AIDLPlayerService extends Service {
	
    private MediaPlayer mediaPlayer;
    
    private static TestControl mTestControl;
    
    private final IBinder binder = new ServiceStub(this);
    static class ServiceStub extends MyMusicControlService.Stub {
        WeakReference<AIDLPlayerService> mService;
        
        ServiceStub(AIDLPlayerService service) {
            mService = new WeakReference<AIDLPlayerService>(service);
        }

		public void pause() throws RemoteException  {
			mService.get().pause();
			
		}

		public void play() {
			mService.get().play();
			
		}

		public void stop() {
			mService.get().stop();
		}

		public void registerTestCall(TestControl cb) throws RemoteException {
			mTestControl = cb;
		}
    }
    
    
	public void pause() throws RemoteException {
		Log.v("AIDLPlayerService", "in pause()");
		if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
			mediaPlayer.pause();
		}
	}

	public void play()  {
		Log.v("AIDLPlayerService", "in play()");
		if(mediaPlayer==null){
			mediaPlayer = MediaPlayer.create(AIDLPlayerService.this,R.raw.tmp);
			//mediaPlayer.start();
			mediaPlayer.setLooping(false);
		}if(!mediaPlayer.isPlaying()){
			//mediaPlayer = MediaPlayer.create(AIDLPlayerService.this,R.raw.tmp);
			mediaPlayer.start();
		}
		
		new Thread(new Runnable(){
			public void run() {
				try {
					Thread.sleep(1000);
				}catch (InterruptedException e){
					e.printStackTrace();
				}
				try {
					mTestControl.setStatus(Integer.toString(mediaPlayer.getCurrentPosition()));
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	public void stop()  {
		Log.v("AIDLPlayerService", "in  stop()");
		if(mediaPlayer!=null){
			//mediaPlayer = MediaPlayer.create(AIDLPlayerService.this,R.raw.tmp);
			mediaPlayer.stop();
			try {
				mediaPlayer.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	public IBinder onBind(Intent arg0) {
		Log.v("AIDLPlayerService", "in  onBind(Intent arg0)");
		return binder;
	}

	@Override
	protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
		Log.v("AIDLPlayerService", "in  dump(0)");
		super.dump(fd, writer, args);
	}

	@Override
	protected void finalize() throws Throwable {
		Log.v("AIDLPlayerService", "in  finalize()");
		super.finalize();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.v("AIDLPlayerService", "in  onConfigurationChanged(Configuration newConfig)");
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onCreate() {
		Log.v("AIDLPlayerService", "in  onCreate()");
		super.onCreate();
	}

	@Override
	public void onLowMemory() {
		Log.v("AIDLPlayerService", "in  onLowMemory()");
		super.onLowMemory();
	}

	@Override
	public void onRebind(Intent intent) {
		Log.v("AIDLPlayerService", "in  onRebind(Intent intent)");
		super.onRebind(intent);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.v("AIDLPlayerService", "in  onStart(Intent intent, int startId)");
		super.onStart(intent, startId);
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.v("AIDLPlayerService", "in  onUnbind(Intent intent)");
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		Log.v("AIDLPlayerService", "in onDestroy()");
		super.onDestroy();
		if(mediaPlayer!=null){
			mediaPlayer.stop();
			mediaPlayer.release();
		}
	}

}
